#include "process_manager.hpp"
#include "boost/filesystem.hpp"
#include <iostream>
#include "../log_manager.hpp"
bool ProcessManager::addProcess(std::string process_name, std::string bin, std::vector<std::string> args) {
    boost::filesystem::path p(bin);

    if (!p.is_absolute()) {
        bin = (boost::filesystem::current_path() / bin).string();
    }
    std::cout << "Adding process: " << process_name << " " << bin << std::endl;
    // 检查文件是否存在并且可执行
    if (boost::filesystem::exists(p) && access(bin.c_str(), X_OK) == 0) {
        process_map_[process_name] = std::make_shared<ProcessExecutor>(process_name, bin, args);
    } else {
        std::cerr << "The specified binary path is not an executable or does not exist: " << bin << std::endl;
        LogManager::getInstance("log.txt").log(LogManager::LogLevel::ERROR, "The specified binary path is not an executable or does not exist: " + bin);
        return false;
    }
    return true;
}
bool ProcessManager::_startProcess(std::string process_name) {
    try 
    {
        process_map_[process_name]->start();
    } catch (const std::system_error& e) {
        std::cerr << "Error starting process: " << e.what() << std::endl;
        LogManager::getInstance("log.txt").log(LogManager::LogLevel::ERROR, "Error starting process: " + std::string(e.what()));
        return false;
    }

    return true;
}
bool ProcessManager::_stopProcess(std::string process_name) {
    try {
        process_map_[process_name]->stop();
    } catch (const std::system_error& e) {
        std::cerr << "Error stopping process: " << e.what() << std::endl;
        LogManager::getInstance("log.txt").log(LogManager::LogLevel::ERROR, "Error stopping process: " + std::string(e.what()));
        return false;
    }
    return true;
}

bool ProcessManager::startProcess(std::string process_name) {
    auto it = process_map_.find(process_name);
    if (it != process_map_.end()) {
        if (!it->second->isRunning()) {
            return _startProcess(process_name);
        }
    }
    return true;
}
bool ProcessManager::stopProcess(std::string process_name){
    auto it = process_map_.find(process_name);
    if (it != process_map_.end()) {
        if (it->second->isRunning()) {
            return _stopProcess(process_name);
        }
    }
    return true;
}
bool ProcessManager::removeProcess(std::string process_name){
    auto it = process_map_.find(process_name);
    if (it != process_map_.end()) {
        if (it->second->isRunning()) {
            _stopProcess(process_name);
        }
        process_map_.erase(it);
    }
    return true;
}
bool ProcessManager::removeAllProcess(){
    for (auto it = process_map_.begin(); it != process_map_.end(); ++it) {
        if (it->second->isRunning()) {
            _stopProcess(it->first);
        }
    }
    process_map_.clear();
    return true;
}
int ProcessManager::InspectionApps(){
    //std::cout << "InspectionApps size: " << process_map_.size() << std::endl;
    // 检查所有进程的状态
    for (auto it = process_map_.begin(); it != process_map_.end(); ++it) {
        if (!it->second->isRunning()) {
            std::cout << "Process " << it->first << " is not running. Restarting..." << std::endl;
            if(_startProcess(it->first) == false) {
                std::cerr << "Failed to restart process: " << it->first << std::endl;
                LogManager::getInstance("log.txt").log(LogManager::LogLevel::ERROR, "Failed to restart process: " + it->first);
                return -1;
            }
        }
    }
    return 0;
}